package com.example.lcpractice.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class MThread {
    public volatile static int x = 0, y = 0;

    public static void main(String[] args) {
    }



    public static void m2() {
        ReentrantReadWriteLock lockX = new ReentrantReadWriteLock(true);
        ReentrantReadWriteLock lockY = new ReentrantReadWriteLock(true);
        Thread thread1 = new Thread(() -> {
            lockX.writeLock().lock();
            int tmpX = MThread.x;
            x = x - 2;
            x = tmpX;
            lockX.writeLock().unlock();
        });
        Thread thread2 = new Thread(() -> {
            lockX.readLock().lock();
            int localX = x;
            lockY.writeLock().lock();
            int localY = y;
            y = localY + 2;
            lockY.writeLock().unlock();
            lockX.readLock().unlock();
        });

    }




}
